//
//  SVProgressHUD+Extension.m
//  OC_Poj_demo
//
//  Created by weixin on 2018/6/24.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "SVProgressHUD+Extension.h"

@implementation SVProgressHUD (Extension)

+ (void)showMsg:(NSString *)msg dismissInterval:(NSTimeInterval)interval {
    //黑底白字
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showImage:nil status:msg];
    [SVProgressHUD dismissWithDelay:interval];
}


+ (void)loadding {
    [SVProgressHUD setDefaultMaskType:1];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setBackgroundColor:[UIColor whiteColor]];
    [SVProgressHUD setForegroundColor:baseColorTheme];
    [SVProgressHUD show];

    [SVProgressHUD dismissWithDelay:NetTimeoutInterval];
}

+ (void)loaddingWithMsg:(NSString *)msg {
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    
    UIImage *image = [self imageWithGIFData:imageData];
    //黑底白字
    [SVProgressHUD dismiss];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setInfoImage:image];
    [SVProgressHUD setMaximumDismissTimeInterval:NetTimeoutInterval];
    [SVProgressHUD setMinimumDismissTimeInterval:NetTimeoutInterval];

    [SVProgressHUD showInfoWithStatus:msg];
}


+ (UIImage *)imageWithGIFData:(NSData *)data {
    if (!data) return nil;
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    size_t count = CGImageSourceGetCount(source);
    UIImage *animatedImage;
    if (count <= 1) {
        animatedImage = [[UIImage alloc] initWithData:data];
    } else {
        NSMutableArray *images = [NSMutableArray array];
        NSTimeInterval duration = 0.0f;
        for (size_t i = 0; i < count; i++) {
            // 拿出了Gif的每一帧图片
            CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);

            // 将每帧图片添加到数组中
            [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
            
            // 释放真图片对象
            CFRelease(image);
            
        }
        // 设置动画时长
        if (!duration) {
            duration = (1.0f / 10.0f) * count;
        }
        animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }
    // 释放源Gif图片
    CFRelease(source);
    return animatedImage;
}


+(void)showSVProgressHUD{
    
    /*
     *0.默认样式，带有黑色文本的白色HUD,HUD背景将会模糊
     *1.黑色HUD和白色文本，HUD背景将会模糊
     *2.使用前和背景色属性
     */
    [SVProgressHUD setDefaultStyle:1];
    
    /*
     *1.背景透明
     *4.背景圆形摄像头黑
     *5.背景全黑
     */
    [SVProgressHUD setDefaultMaskType:1];
    
    /*
     *1.画圈样式
     *2.菊花样式
     */
    [SVProgressHUD setDefaultAnimationType:0];
    
    //禁止点击
    [SVProgressHUD setDefaultMaskType:2];
    
    //加载父视图
    //    [SVProgressHUD setContainerView:self];
    
    //外边框大小
    [SVProgressHUD setMinimumSize:CGSizeMake(50,50)];
    
    //圈的厚度默认为5
    [SVProgressHUD setRingThickness:3];
    
    //圈的半径
    //    [SVProgressHUD setRingRadius:50];
    
    //没有文本时候的半径
    [SVProgressHUD setRingNoTextRadius:20];
    
    //圆角
    [SVProgressHUD setCornerRadius:10];
    
    //字体大小
    //     [SVProgressHUD setFont:[UIFont systemFontOfSize:20]];
    
    //圈圈颜色
    [SVProgressHUD setForegroundColor:[UIColor redColor]];
    
    //背景颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
    
    //设置最小结束时间
    [SVProgressHUD setMinimumDismissTimeInterval:1];
    
    //设置最大结束时间
    [SVProgressHUD setMaximumDismissTimeInterval:2];
    
    [SVProgressHUD setFadeInAnimationDuration:0.2];
    
    [SVProgressHUD setFadeOutAnimationDuration:1];
    
    [SVProgressHUD setMaxSupportedWindowLevel:1];
    
    [self show];
}

+(void)dismissVProgressHud:(CGFloat)time{
    [SVProgressHUD setDefaultMaskType:1];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setBackgroundColor:[UIColor whiteColor]];
    [SVProgressHUD setForegroundColor:baseColorTheme];
    [SVProgressHUD show];

    [SVProgressHUD dismissWithDelay:time];
}


@end
